package GameOntology;

import java.util.ArrayList;

public class Bullet extends GameObject {
    private int speed = 7;
    private String direction="up";

    GameWindow gameWindow ;
    private String team;
    public Bullet( String img, int x, int y,String direction,String team,GameWindow gameWindow) {

        super(17,17, img, x, y);
        this.direction=direction;
        this.team=team;
        this.gameWindow=gameWindow;
    }

    public Bullet() {
    }



    public void move(){
        int x=this.getX();
        int y=this.getY();
        switch (this.direction) {
            case "up":
                y-=speed;
                if (text(x,y)){
                    this.setY(y);
                    this.renewalRec(this.getX(),this.getY(),17,17);
                }
                break;
            case "down":
                y+=speed;
                if (text(x,y)){
                    this.setY(y);
                    this.renewalRec(this.getX(),this.getY(),17,17);
                }
                break;
            case "left":
                x-=speed;
                if (text(x,y)){
                    this.setX(x);
                    this.renewalRec(this.getX(),this.getY(),17,17);
                }
                break;
            case "right":
                x+=speed;
                if (text(x,y)){
                    this.setX(x);
                    this.renewalRec(this.getX(),this.getY(),17,17);
                }
                break;
            default:break;
        }
    }
    public void hit(){
        switch (team){
            case "player":
                ArrayList<ComputerPlayer> computerPlayers = this.gameWindow.computerPlayers;
                for (int i = 0; i < computerPlayers.size(); i++) {
                    if (this.getRec().intersects(computerPlayers.get(i).getRec())){
                        this.gameWindow.computerPlayers.remove(computerPlayers.get(i));
                        this.gameWindow.removeBullets.add(this);
                        break;
                    }
                }
                break;
            case "computer":
                ArrayList<Player> players =this.gameWindow.players;
                if (!players.isEmpty()&&this.getRec().intersects(players.get(0).getRec())){
                    this.gameWindow.players.remove(players.get(0));
                    this.gameWindow.removeBullets.add(this);
                }
                break;
        }
    }
    public boolean text(int x, int y){
        if (x<0||x>913||y<0||y>760){
            this.gameWindow.removeBullets.add(this);
            return false;
        }
        return true;
    }
}
